Why is the error occurring when trying to use a list in the range function